package com.mojiayi.learn.log.log4j2;

import com.mojiayi.learn.log.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 用于演示log4j2的简单例子
 */
@RestController
@RequestMapping("/log4j2")
public class OutputLogViaLog4j2Controller {
    /**
     * 日志输出对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OutputLogViaLog4j2Controller.class);

    /**
     * 用于测试log4j2日志输出的简单方法
     *
     * @return 返回一个简单的用户对象
     */
    @GetMapping("/print")
    public User print() {
        MDC.put("traceId", UUID.randomUUID().toString().replaceAll("-", ""));
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            LOGGER.info("print method {}", i);
        }
        long finishTime = System.currentTimeMillis();
        LOGGER.info("print 100 lines log info that contain caller class info,cost time in millseconds is {}",
                (finishTime - startTime));
        return new User("testname");
    }
}
